package com.qf.service.impl;

import com.qf.entity.Income;
import com.qf.entity.Task;
import com.qf.enums.OAExceptionEnum;
import com.qf.exception.OAException;
import com.qf.mapper.IncomeMapper;
import com.qf.mapper.TaskMapper;
import com.qf.service.IncomeService;
import com.qf.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
@Service
@Slf4j
public class IncomeServiceImpl implements IncomeService {

    @Resource
    private IncomeMapper incomeMapper;

    @Resource
    private TaskMapper taskMapper;

    @Override
    @Transactional
    public Integer add(Income income) {
        //初始化默认审核状态,待审核
        income.setAuditStatus(0);
        income.setAuditTime(new Date());
        //初始化默认的审核任务
        income.setTaskId(6);
        Integer count = incomeMapper.insertInfo(income);
        if (count != 1){
            log.info("【添加失败】 收入流程添加失败 count = {}",count);
            throw new OAException(OAExceptionEnum.ADD_INCOME_ERROR.getCode(),OAExceptionEnum.ADD_INCOME_ERROR.getMsg());
        }

        Task param = new Task();
        param.setApplyName(income.getRealName());
        param.setApplyTime(income.getAuditTime());
        param.setTaskId(income.getTaskId());
        param.setStatus(income.getAuditStatus());
        param.setUserId(income.getUserId());
        param.setEventId(income.getIncomeId());
        int num = taskMapper.insert(param);

        return count;
    }
}
